package pl.nazaweb.imedproplus.rest;

import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import pl.nazaweb.imedproplus.business.service.PatientService;
import pl.nazaweb.imedproplus.rest.pojo.PatientPojo;
import pl.nazaweb.imedproplus.rest.pojo.ResponsePojo;

@Stateless
@Path("/json/patient")
public class Patient extends AbstractRest<PatientPojo> {

    @EJB
    private PatientService patientService;

    @GET
    @Path("/get")
    @Produces(MediaType.APPLICATION_JSON)
    public ResponsePojo getPatientList() {
        return this.getEntityPojoList();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/create")
    public ResponsePojo createPatients(List<PatientPojo> patientPojos) {
        return this.createEntities(patientPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/destroy")
    public ResponsePojo removePatients(List<PatientPojo> patientPojos) {
        return this.destroyEntities(patientPojos);
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    @Produces(MediaType.APPLICATION_JSON)
    @SuppressWarnings("unchecked")
    @Path("/update")
    public ResponsePojo updatePatients(List<PatientPojo> patientPojos) {
        return this.updateEntities(patientPojos);
    }

    @Override
    public List<PatientPojo> getEntityPojoListResult() {
        List<PatientPojo> result = new ArrayList<PatientPojo>();
        for (pl.nazaweb.imedproplus.model.entity.Patient patient : this.patientService.getPatientList()) {
            result.add(new PatientPojo(patient));
        }
        return result;
    }

    @Override
    public void persistNewEnities(List<PatientPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Patient> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Patient>();
        for (PatientPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.patientService.addNewPatients(result);
    }

    @Override
    public void removeEnities(List<PatientPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Patient> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Patient>();
        for (PatientPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.patientService.removePatients(result);
    }

    @Override
    public void updateEnities(List<PatientPojo> pojos) {
        List<pl.nazaweb.imedproplus.model.entity.Patient> result = new ArrayList<pl.nazaweb.imedproplus.model.entity.Patient>();
        for (PatientPojo pojo : pojos) {
            result.add(pojo.getEntity());
        }
        this.patientService.updatePatients(result);
    }
}
